package com.lxj.student_security.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxj.student_security.common.CommonResult;
import com.lxj.student_security.entity.Permission;
import com.lxj.student_security.entity.vo.Tree;
import com.lxj.student_security.mapper.PermissionMapper;
import com.lxj.student_security.service.PermissionService;
import com.lxj.student_security.service.UserService;
import com.lxj.student_security.utils.BuildTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private UserService userService;

    @Override
    public CommonResult searchAll(Permission permission) {
        PageHelper.startPage(permission.getPageNum(), permission.getPageSize());
        PageInfo<Permission> pageInfo = new PageInfo<>(this.permissionMapper.searchAll(permission));
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("number", pageInfo.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult edit(Permission permission) {
        int count = permissionMapper.edit(permission);
        if (count != 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("修改失败");
    }

    @Override
    public CommonResult add(Permission permission) {
        int count = permissionMapper.add(permission);
        if (count != 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("新增失败");
    }

    @Override
    public CommonResult delete(Integer[] id) {
        int count = permissionMapper.delete(id);
        if (count != 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("删除失败");
    }

    @Override
    public CommonResult findById(Integer id) {
        Permission per = permissionMapper.findById(id);
        Assert.notNull(per, "查询失败");
        return CommonResult.success(per);
    }

    /**
     * 获取树形权限结构
     *
     * @return
     */
    @Override
    public Tree<Permission> tree() {
        //定义一个存储Permission的集合
        List<Tree<Permission>> trees = new ArrayList<>();
        List<Permission> list = permissionMapper.findAll();
        for (Permission all : list) {
            Tree<Permission> tree = new Tree<>();
            tree.setId(all.getId());
            tree.setPid(all.getPid());
            tree.setTitle(all.getName());
            Map<String, Object> map = new HashMap<>(16);
            Integer perId = all.getPid();
            trees.add(tree);
        }
        Tree<Permission> t = BuildTree.build(trees);
        System.out.println(t);
        return t;
    }

    /**
     * 角色分配权限查询所有可权限
     *
     * @return
     */
    @Override
    public CommonResult findAll() {
        List<Permission> list = permissionMapper.findAll();
        return CommonResult.success(list);
    }

    @Override
    public Tree<Permission> getMenu(String username) {
        Integer id = permissionMapper.getId(username);
        List<Tree<Permission>> trees = new ArrayList<>();
        List<Permission> list = userService.getPermissionsByUserId(id);
//        HashSet<Permission> permissions = new HashSet<>(list);
        for (Permission all : list) {
            Tree<Permission> tree = new Tree<>();
            tree.setId(all.getId());
            tree.setPid(all.getPid());
            tree.setTitle(all.getName());
            tree.setSrc(all.getSrc());
            Map<String, Object> map = new HashMap<>(16);
            Integer perId = all.getPid();
            trees.add(tree);
        }
        Tree<Permission> t = BuildTree.build(trees);
        System.out.println(t);
        return t;
    }
}
